package com.program.facesigninsystem.service.impl;

import com.program.facesigninsystem.entity.User;
import com.program.facesigninsystem.entity.UserInfo;
import com.program.facesigninsystem.service.IRedisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sxw
 */
@Service
public class RedisServiceImpl implements IRedisService {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void addUser(String uid, String ip, String loginTime, String errorCounts) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        if(map==null) {
            map= new HashMap<>();
        }
        map.put("uid",uid);
        map.put("ip",ip);
        map.put("loginTime",loginTime);
        map.put("errorCounts",errorCounts);
        map.put("nextLogin","");
        vo.set(uid, map);
    }

    @Override
    public void addUserInfo(UserInfo userInfo,String Class) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(userInfo.getUid());
        if(map==null) {
            map= new HashMap<>();
        }
        map.put("uid",userInfo.getUid());
        map.put("openid",userInfo.getOpenid());
        map.put("type",userInfo.getType()+"");
        map.put("name",userInfo.getName());
        map.put("studentId",userInfo.getStudentId());
        map.put("nickname",userInfo.getNickname());
        map.put("sex",userInfo.getSex());
        map.put("age",userInfo.getAge()+"");
        map.put("address",userInfo.getAddress());
        map.put("phone",userInfo.getPhone());
        map.put("email",userInfo.getEmail());
        map.put("university",userInfo.getUniversity());
        map.put("academy",userInfo.getAcademy());
        map.put("major",userInfo.getMajor());
        map.put("class",Class);
        map.put("avatar",userInfo.getAvatar());
        map.put("uid1",userInfo.getUid1());
        map.put("bindId",userInfo.getBindId()+"");
        vo.set(userInfo.getUid(), map);
    }

    @Override
    public User findByUId(String uid) {
        ValueOperations vo=redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        if(map!=null){
            String ip=(String)map.get("ip");
            String loginTime=(String)map.get("loginTime");
            String errorCounts=(String)map.get("errorCounts");
            String nextLogin=(String)map.get("nextLogin");
            User user=new User(uid,ip,loginTime,errorCounts,nextLogin);
            return user;
        }else {
            return null;
        }
    }

    @Override
    public UserInfo findUser(String uid) {
        ValueOperations vo=redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        if(map!=null){
            String openid=(String)map.get("openid");
            String name =(String)map.get("name");
            String studentId = (String)map.get("studentId");
            String nickname =(String)map.get("nickname");
            int type=Integer.parseInt((String)map.get("type"));
            String sex=(String)map.get("sex");
            int age=Integer.parseInt((String)map.get("age"));
            String address=(String)map.get("address");
            String phone=(String)map.get("phone");
            String email=(String)map.get("email");
            String university=(String)map.get("university");
            String academy=(String)map.get("academy");
            String major=(String)map.get("major");
            String Class=(String)map.get("class");
            String avatar=(String)map.get("avatar");
            String uid1=(String)map.get("uid1");
            int bindId=Integer.parseInt((String)map.get("bindId"));
            return new UserInfo(uid,openid,name,studentId,nickname,type,sex,age,address,phone,email,university,academy,major,Class,avatar,uid1,bindId);
        }else {
            return null;
        }
    }

    @Override
    public void updateBindid(String uid,String bindid) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("bindId")){
            map.put("bindId",bindid);
        }
        vo.set(uid, map);
    }

    /*本地测试时用，将用户信息直接塞到redis，避免redis由于没有数据而报错*/
//    @Override
//    public void updateUser(String uid, String name, String studentId, String nickname, int type, String major, String uclass,
//                           String university, String academy, String avatar, String sex, int age, String address,
//                           String phone, String email) {
//        // 获取 Redis 操作对象
//        ValueOperations<String, Map> vo = redisTemplate.opsForValue();
//        Map<String, Object> map = vo.get(uid);
//
//        // 如果 Redis 中没有数据，调用 addUserInfo 方法
//        if (map == null) {
//            System.out.println("Redis 中未找到用户数据，uid: {" + uid + "}，将添加用户信息到 Redis");
//            UserInfo userInfo = new UserInfo(uid);
//            addUserInfo(userInfo, uclass);
//            //return;
//        }
//
//        // 如果存在数据，直接更新字段
//        map.put("name", name);
//        map.put("studentId", studentId);
//        map.put("nickname", nickname);
//        map.put("type", String.valueOf(type)); // 转换为字符串
//        map.put("major", major);
//        map.put("class", uclass);
//        map.put("university", university);
//        map.put("academy", academy);
//        map.put("avatar", avatar);
//        map.put("sex", sex);
//        map.put("age", String.valueOf(age)); // 转换为字符串
//        map.put("address", address);
//        map.put("phone", phone);
//        map.put("email", email);
//        // 写回 Redis
//        vo.set(uid, map);
//    }

    @Override
    public void updateUser(String uid, String name, String studentId, String nickname,int type, String major, String uclass, String university, String academy, String avatar, String sex, int age, String address, String phone, String email) {
        ValueOperations<String, Map> vo = redisTemplate.opsForValue();
        Map<String, Object> map = vo.get(uid);
        assert map != null;
        if (map.containsKey("name")){
            map.put("name",name);
        }
        if (map.containsKey("studentId")){
            map.put("studentId",studentId);
        }
        if (map.containsKey("nickname")){
            map.put("nickname",nickname);
        }
        if(map.containsKey("type")){
            map.put("type",type+"");
        }
        if (map.containsKey("major")){
            map.put("major",major);
        }
        if(map.containsKey("class")){
            map.put("class",uclass);
        }
        if (map.containsKey("university")){
            map.put("university",university);
        }
        if(map.containsKey("academy")){
            map.put("academy",academy);
        }
        if (map.containsKey("avatar")){
            map.put("avatar",avatar);
        }
        if(map.containsKey("sex")){
            map.put("sex",sex);
        }
        if (map.containsKey("age")){
            map.put("age",age+"");
        }
        if(map.containsKey("address")){
            map.put("address",address);
        }
        if (map.containsKey("phone")){
            map.put("phone",phone);
        }
        if(map.containsKey("email")){
            map.put("email",email);
        }
        vo.set(uid, map);
    }

    @Override
    public void updateByUId(String uid, String ip, String loginTime) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("ip")){
            map.put("ip",ip);
        }
        if(map.containsKey("loginTime")){
            map.put("loginTime",loginTime);
        }
        vo.set(uid, map);
    }

    @Override
    public void updateLogin(String uid, String errorCounts, String nextLogin) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("errorCounts")){
            map.put("errorCounts",errorCounts);
        }
        if(map.containsKey("nextLogin")){
            map.put("nextLogin",nextLogin);
        }
        vo.set(uid, map);
    }

    @Override
    public void addRank(List<Map<String, Object>> rank) {
        ValueOperations vo = redisTemplate.opsForValue();
        vo.set("Rank", rank);
    }

    @Override
    public List<Map<String, Object>> getRank() {
        ValueOperations vo=redisTemplate.opsForValue();
        List<Map<String, Object>> rank=(List<Map<String, Object>>)vo.get("Rank");
        if(rank!=null){
            return rank;
        }else {
            return null;
        }
    }
}
